//
//  SWUploadHairProsViewController.m
//  YiYueHairdresser
//
//  Created by Sky on 2018/10/31.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "SWUploadHairProsViewController.h"
#import "YLAddImageView.h"
#import "SWUploadHairProMenueView.h"
#import "YLNumberTextView.h"
#import "UploadManage.h"
#import "SWFeatureModel.h"
@interface SWUploadHairProsViewController ()<YLAddImageViewDelegate>

@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIView *mainView;
@property(nonatomic, strong) UIButton *saveBtn;      //保存
@property(nonatomic, strong) YLAddImageView *addImageView;
@property(strong,nonatomic) NSMutableArray * imageArray;
@property(nonatomic, strong) SWUploadHairProMenueView *menueViewA;   //头发
@property(nonatomic, strong) SWUploadHairProMenueView *menueViewB;   //脸型
@property(strong ,nonatomic) YLNumberTextView * textView;
@property(nonatomic, strong) UIView *describeView;
@property(nonatomic, strong) UILabel *numLab;
@property(nonatomic, strong) NSString *faceTypeIndex;              //脸型
@property(nonatomic, strong) NSString *hairTypeIndex;              //发型
@property (nonatomic, strong) NSMutableArray <UIImage *>*imagesArray;
@property(nonatomic, strong) NSArray <SWFeatureModel*>*faceTypeArray;              //脸型数据
@property(nonatomic, strong) NSArray <SWFeatureModel*>*hairTypeArray;              //发型数据
@end

@implementation SWUploadHairProsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
    [self getDataFromeService];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 初始化
- (void)initUI {
    
    self.title = @"上传作品";
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.saveBtn];
    self.scrollView.backgroundColor = UIColor.groupTableViewBackgroundColor;
   
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.inset(0);
        make.bottom.equalTo(self.saveBtn.mas_top);
    }];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.inset(0);
        make.height.mas_equalTo(50);
    }];


    _imageArray = [NSMutableArray array];
    _addImageView = [[YLAddImageView alloc] initWithImageArray:_imageArray type:1 title:@"作品展示图" VC:self];
    _addImageView.height = 127;
    _addImageView.mj_y = 0;
    _addImageView.delegate = self;
    _addImageView.isAutomatic = YES;
    [self.scrollView addSubview:_addImageView];
    _addImageView.backgroundColor = UIColor.whiteColor;
    
    [self.scrollView addSubview:self.menueViewA];
    [self.scrollView addSubview:self.menueViewB];
    [self.scrollView addSubview:self.describeView];
    
//    self.menueViewA.titleArray = @[@"短发", @"中发", @"长发"];
//    self.menueViewB.titleArray = @[@"圆脸", @"方脸", @"瓜子脸",@"鹅蛋脸", @"尖脸"];
    
    _faceTypeIndex = @"";
    _hairTypeIndex = @"";
    
    [self getDataFromeService];
    [self getDataFromeServiceB];
    [self addStatus];
}


- (void)getDataFromeService {
    
    BasicRequest * request = [[BasicRequest alloc] initWithParams:@{}];
    request.URL = [NSString stringWithFormat:@"%@%@",SWBaseUrlA, @"opusType/getHairstyle"];
    request.requestMethod = HttpRequestMethodGET;
    [request startRequestWithSuccess:^(NSURLSessionTask *task, id responseObject) {
        NSNumber *code = responseObject[@"code"];
        NSDictionary *dict = responseObject[@"data"];
        if (code.integerValue == 200) {
            
            if(![dict isKindOfClass:[NSNull class]]){
                
                _hairTypeArray = [NSArray modelArrayWithClass:[SWFeatureModel class] json:dict[@"data"]];
                self.menueViewA.titleArray = [_hairTypeArray valueForKeyPath:@"name"];
                [self addStatus];
            }
            
        }
    } Failure:nil];
    
}

- (void)getDataFromeServiceB {
    
    BasicRequest * request = [[BasicRequest alloc] initWithParams:@{}];
    request.URL = [NSString stringWithFormat:@"%@%@",SWBaseUrlA, @"opusType/getFeature"];
    request.requestMethod = HttpRequestMethodGET;
    [request startRequestWithSuccess:^(NSURLSessionTask *task, id responseObject) {
        NSNumber *code = responseObject[@"code"];
        NSDictionary *dict = responseObject[@"data"];
        if (code.integerValue == 200) {
            
            if(![dict isKindOfClass:[NSNull class]]){
                
                _faceTypeArray = [NSArray modelArrayWithClass:[SWFeatureModel class] json:dict[@"data"]];
                self.menueViewB.titleArray = [_faceTypeArray valueForKeyPath:@"name"];
                [self addStatus];
            }
            
        }
    } Failure:nil];
    
}





- (void)addStatus {
    
    self.menueViewA.mj_y = CGRectGetMaxY(_addImageView.frame) + 10;
    self.menueViewB.mj_y = CGRectGetMaxY(self.menueViewA.frame) + 10;
    self.describeView.mj_y = CGRectGetMaxY(self.menueViewB.frame) + 10;
    
    [self.scrollView setContentSize:CGSizeMake(self.view.width, CGRectGetMaxY(self.describeView.frame))];
}


//代理
- (void)selectImagecomplete {
    
    NSArray *images= [_addImageView.imageArray valueForKeyPath:@"image"];
    _imagesArray = images.mutableCopy;
    [_imagesArray removeLastObject];
    [self addStatus];
}


#pragma mark - 点击事件

/**
 保存

 @param sender 对象
 */
- (void)saveAction:(UIButton *)sender {

     __weak typeof(self)weakSelf = self;
    if (_imagesArray.count < 1) {
        [YLProgressHUDManager showWarning:@"至少一张作品照片"];
        return;
    }
    
    if ([_hairTypeIndex isEqualToString:@""]) {
        [YLProgressHUDManager showWarning:@"请选择发型"];
        return;
    }
    
    if ([_faceTypeIndex isEqualToString:@""]) {
        [YLProgressHUDManager showWarning:@"请选择脸型"];
        return;
    }
    
    NSString * describe = _textView.textView.text;
    if (describe.length < 1 || [describe isEqualToString:@""]) {
        
        [YLProgressHUDManager showWarning:@"请选择填写作品说明"];
        return;
    }
    
    [[UploadManage manager] uploadImageArray:_imagesArray success:^(NSURLSessionTask *task, id responseObject)  {
        NSNumber *code = responseObject[@"code"];
        NSArray *imageurl = responseObject[@"data"];
        if (code.integerValue == 200) {
            
            BasicRequest * request = [[BasicRequest alloc] initWithParams:@{@"stylistId":[YLUserInfo sharedInstance].userID, @"opusPaths":imageurl, @"hairstyleId":_hairTypeIndex, @"featureId":_faceTypeIndex, @"describe":describe}];
            request.URL = [NSString stringWithFormat:@"%@stylistOpus/save",BASEURL];
            request.requestMethod = HttpRequestMethodRawPOST;
            [request startRequestWithSuccess:^(NSURLSessionTask *task, id responseObject) {
                
                NSNumber *code = responseObject[@"code"];
                if (code.integerValue == 200) {
                    [YLProgressHUDManager showSuccess:responseObject[@"message"]];
                   
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.75* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                    });
                }
                
            } Failure:nil];
            
        }
    } failure:^(NSURLSessionTask *task, NSError *error) {
        NSLog(@"error------ %@",error);
    }];
    
    
   
}


#pragma mark - 懒加载
-(UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [UIScrollView new];
    }
    return _scrollView;
}

- (UIView *)mainView {
    if (_mainView == nil) {
        _mainView = [UIView new];
        _mainView.backgroundColor = UIColor.groupTableViewBackgroundColor;
    }
    return _mainView;
}

-(UIButton *)saveBtn {
    if (_saveBtn == nil) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _saveBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _saveBtn.backgroundColor = RGBCOLOR(252, 102, 33);
        [_saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

- (SWUploadHairProMenueView *)menueViewA {
    WeakSelf
    if (_menueViewA == nil) {
        _menueViewA = [[SWUploadHairProMenueView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
        _menueViewA.titleLab.text = @"头发";
        _menueViewA.clickBlock = ^(NSInteger index) {
            NSLog(@"选择发型下标%ld", (long)index);
            _hairTypeIndex = weakSelf.hairTypeArray[index].id;
        };
    }
    return _menueViewA;
}

- (SWUploadHairProMenueView *)menueViewB {
    WeakSelf
    if (_menueViewB == nil) {
        _menueViewB = [[SWUploadHairProMenueView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
        _menueViewB.titleLab.text = @"脸型";
        _menueViewB.clickBlock = ^(NSInteger index) {
            NSLog(@"选择脸型下标%ld", (long)index);
            
            _faceTypeIndex = weakSelf.faceTypeArray[index].id;
        };
    }
    return _menueViewB;
}

- (UIView *)describeView {
    if (_describeView == nil) {
       
        _describeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 150)];
        _describeView.backgroundColor = UIColor.clearColor;
        
        UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _describeView.width, 30)];
        mainView.backgroundColor = UIColor.whiteColor;
        UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, mainView.width, 30)];
        titleLab.text = @"作品描述";
        titleLab.font = [UIFont systemFontOfSize:17];
        titleLab.textColor = UIColor.blackColor;
        [mainView addSubview:titleLab];
        [_describeView addSubview:mainView];
        WeakSelf
        _textView = [[YLNumberTextView alloc] initWithPlacehoderStr:@"简单说明作品的特色、风格等" maxLength:100];
        _textView.backgroundColor = WhiteColor;
        _textView.frame = CGRectMake(0, CGRectGetMaxY(titleLab.frame), _describeView.width, 100);
        _textView.backNUMStrBlock = ^(NSString *content) {
            weakSelf.numLab.text = content;
        };
        [_describeView addSubview:_textView];
        
        UILabel *titleLab1 = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_textView.frame), _describeView.width - 20, 20)];
        titleLab1.text = @"0/100";
        titleLab1.font = [UIFont systemFontOfSize:14];
        titleLab1.textColor = UIColor.lightGrayColor;
        titleLab1.textAlignment = NSTextAlignmentRight;
        self.numLab = titleLab1;
        
        [_describeView addSubview:titleLab1];
    }
    return _describeView;
}
@end
